<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--      筛选-->
      <!--      分栏24占满 -->
      <el-col :span="24" :xs="24">
        <el-form v-if="!loading" label-position="top" :rules="rules" :model="article" ref="form" size="small"  label-width="100px">
          <el-row :gutter="20" :span="24">
            <el-col :span="12">
              <el-form-item label="标题" prop="articleTitle">
                <el-input
                  v-model="article.articleTitle"
                  placeholder="请输入标题"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item style="width: 100%" label="分类" prop="type">
                <reference :component="() => import('@/views/type/index')"
                           title="分类参照"
                           v-model="article.type" />
              </el-form-item>
            </el-col >
          </el-row>
          <el-form-item label="封面地址" prop="articleCover">
            <el-input
              v-model="article.articleCover"
              placeholder="请输入标题"
              clearable
            />
          </el-form-item>
          <el-form-item label="文章" prop="articleContent">
            <md-editor ref="descriptionMd" :default-data="article.articleContent"/>
          </el-form-item>
          <el-row :gutter="20" :span="24">
            <el-col :span="12">
              <el-form-item label="权限" prop="auth">
                <el-select
                  v-model="article.auth"
                  placeholder="选择权限"
                >
                  <el-option
                    v-for="dict in authList"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="置顶" prop="isTop">
                <el-input-number v-model="article.isTop"  label="置顶"></el-input-number>
              </el-form-item>
            </el-col >
          </el-row>
          <el-row :gutter="20" :span="24">
            <el-col :span="12">
              <el-form-item label="是否启用" prop="status">
                <el-switch
                  v-model="article.status"
                  :active-value="0"
                  :inactive-value="1"
                  active-color="#13ce66"
                  inactive-color="#ff4949">
                </el-switch>
              </el-form-item>
            </el-col >
            <el-col :span="12">
              <el-form-item label="开启评论" prop="status">
                <el-switch
                  v-model="article.isComment"
                  :active-value="1"
                  :inactive-value="0"
                  active-color="#13ce66"
                  inactive-color="#ff4949">
                </el-switch>
              </el-form-item>
            </el-col >
          </el-row>

        </el-form>
      </el-col>
      <el-col :span="24">
        <el-button :loading="savaLoading" type="sussess" @click="saveOrUpdate" style="width: 100%">保存</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import MdEditor from '@/components/mdEditor'
import Reference from '@/components/Reference'
import * as api from "@/api/article"
export default {
  name: 'addProblemList',
  components: { Reference, MdEditor },
  data(){
    return{
      loading:false,
      savaLoading:false,
      article : {
        articleTitle:undefined,
        articleCover:null,
        articleContent:undefined,
        auth:1,
        isTop:0,
        status:0,
        isComment:1,
        typeId:'',
        type:{},
      },
      authList:[
        {
          label:"公开",
          value:1,
        },
        {
          label:"私有",
          value:2,
        },
        {
          label:"草稿",
          value:3,
        }
      ],
      rules: {
        articleTitle: [{ required: true, message: "标题不能不为空", trigger: "blur", },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }],
        articleContent: {
          required: true,
          message: "描述不能为空",
          trigger: "blur",
        },
        type: {
          required: true,
          message: "描述不能为空",
          trigger: "blur",
        },
        isTop: {
          required: true,
          message: "展示顺序不能为空",
          trigger: "blur",
        },
      },
    }
  },
  created() {
    const id = this.$route.query.id;
    console.log(id)
    if(id!=undefined && id!=null){
      this.loading = true;
      api.query(id).then(res => {
        this.article = res.data
      }).finally(() => {
        this.loading = false;
      })
    }
  },
  methods:{
    saveOrUpdate(){
      this.article.articleContent = this.$refs.descriptionMd.getValue();
      this.$refs["form"].validate(valid => {
        if (valid) {
          let params = this.article
          params.typeId = this.article.type.id
          if(params.typeId == null){
            this.$message.info("分类不能为空！")
            return;
          }
          if(params.articleContent == null || params.articleContent == ""){
            this.$message.info("文章内容不能为空！")
            return;
          }
          this.savaLoading = true;
          api.saveOrUpdate(params).then(res => {
            this.$message.success("保存成功！")
          }).catch(err => {

          }).finally(() => {
            this.savaLoading = false;
          })
        }else{

          this.$message.error("校验失败！")
        }
      });
    },

  }
}
</script>

<style scoped>

</style>
